package com.baosight.risk.service.rire.impl;

import com.baosight.risk.api.rire.RiwaEquityInvestService;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.StringUtils;
import com.baosight.risk.mapper.riwa.RiwaEquityInvestMainMapper;
import com.baosight.risk.mapper.riwa.RiwaEquityInvestMapper;
import com.baosight.risk.mapper.xtzy.XtzyCompanyMapper;
import com.baosight.risk.service.riwa.entity.RiwaEquityInvest;
import com.baosight.risk.service.riwa.entity.RiwaEquityInvestMain;
import com.baosight.risk.service.riwa.vo.RiwaEquityInvestVO;
import com.baosight.risk.service.xtzy.entity.XtzyCompany;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.lucene.document.StringField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class RiwaEquityInvestServiceImpl extends BaseServiceImpl<RiwaEquityInvest> implements RiwaEquityInvestService {

    @Autowired
    private RiwaEquityInvestMainMapper investMainMapper;
    @Autowired
    private RiwaEquityInvestMapper investMapper;
    @Autowired
    private XtzyCompanyMapper companyMapper;


    @Override
    public List<RiwaEquityInvestMain> selectList(RiwaEquityInvestMain equityInvestMain) {
        String acctYearQuarter = equityInvestMain.getAcctYear();
        if (org.apache.commons.lang.StringUtils.isNotEmpty(acctYearQuarter)) {
            String acctYear = acctYearQuarter.substring(0, 4);
            equityInvestMain.setAcctYear(acctYear);
        }
        List<RiwaEquityInvestMain> mainList = investMainMapper.selectList(equityInvestMain);
        if (CollectionUtils.isNotEmpty(mainList)) {
            for (RiwaEquityInvestMain main : mainList) {
                main.setExt1(main.getDeptName() + main.getAcctYear() + "年长期股权投资收益跟踪");
            }
        }
        return mainList;
    }

    @Override
    public RiwaEquityInvestMain selectByYearAndQuarter(RiwaEquityInvestMain main) {
        return investMainMapper.selectByYearAndQuarter(main);
    }

    @Override
    public RiwaEquityInvestVO getById(String id) {
        RiwaEquityInvestMain main = investMainMapper.selectByPrimaryKey(id);
        RiwaEquityInvestVO investVO = new RiwaEquityInvestVO();
        BeanUtils.copyBeanProp(investVO, main);
        List<RiwaEquityInvest> investList = investMapper.selectByMainId(id);
        investVO.setEquityInvestList(investList);
        return investVO;
    }

    @Override
    public RiwaEquityInvestVO saveEquityInvest(RiwaEquityInvestVO main) throws Exception {
        RiwaEquityInvestMain riwaEquityInvestMain = investMainMapper.selectByYearAndQuarter(main);
        if (riwaEquityInvestMain==null) {
            XtzyCompany company = companyMapper.getCompanyInfo(main.getDeptCode());
            //如果公司类型是1或者是2，那么公司和部门都是同样的
            if ("2".equals(company.getPathLevel() + "") || "1".equals(company.getPathLevel() + "")) {
                main.setCompCode(main.getDeptCode());
                main.setCompName(main.getDeptName());
            } else {
                //获取一级子公司或部门
                company = companyMapper.getTopCompany(company.getPathCode(), Short.parseShort("2"));
                main.setCompCode(company.getCompCode());
                main.setCompName(company.getCompName());
            }
            main = (RiwaEquityInvestVO) BeanUtils.initBean(main);
            investMainMapper.insert(main);
        } else {
            investMapper.deleteByMainId(main.getId());
        }
        if (CollectionUtils.isNotEmpty(main.getEquityInvestList())) {
            insertEquityInvest(main.getEquityInvestList(), main.getId());
            //保存后计算
            calculate(main);
            investMainMapper.updateByPrimaryKey(main);
        }
        return main;
    }

    public void insertEquityInvest(List<RiwaEquityInvest> equityInvestLis, String mainId) {
        for (RiwaEquityInvest riwaEquityInvest : equityInvestLis) {
            riwaEquityInvest.setMainGuid(mainId);
            riwaEquityInvest = (RiwaEquityInvest) BeanUtils.initBean(riwaEquityInvest);
            investMapper.insert(riwaEquityInvest);
        }
    }

    public void calculate(RiwaEquityInvestVO main) throws Exception {
        if (null == main.getRoe() || null == main.getRoeDesc() || null == main.getTrackManage()
                || null == main.getInvestDesc() || null == main.getTargetRoe()
                || null == main.getTargetRoeDesc() || null == main.getTargetInvestDesc()
                || null == main.getTargetTrackManage()) {
            log.error("数据不完整，无法计算结果");
            throw new Exception("数据不完整，无法计算结果");
        }
        List<Integer> conclu = new ArrayList<>();
        //-----------------------盈利情况预警结论---------------------------
        /**
         * 达到标准值，亏损面=<20%，得0分，绿色
         * 达到标准值，亏损面>20%，得1分，黄色
         * 未达到标准值，亏损面=<20%，得1分，黄色
         * 未达到标准值，亏损面>20%，得2分，橙色
         */
        //合计盈利和盈利指标的比较结果
        int roe = main.getRoe().compareTo(main.getTargetRoe());
        //合计亏损面和亏损面指标的比较结果
        int roeDesc = main.getRoeDesc().compareTo(main.getTargetRoeDesc());
        //如果结果!=-1,表示未达到标准值
        if (roe > -1) {
            if (roeDesc > 0) {
                //得1分，黄色
                main.setConcluRoe("1");
                conclu.add(1);
            } else {
                //得0分，绿色
                main.setConcluRoe("0");
                conclu.add(0);
            }
        } else {
            //达到标准值
            if (roeDesc > 0) {
                //得2分，橙色
                main.setConcluRoe("2");
                conclu.add(2);
            } else {
                //得1分，黄色
                main.setConcluRoe("1");
                conclu.add(1);
            }
        }
//        if (roe != -1) {
//            //未达到标准值
//            if (roeDesc == 1) {
//                //得1分，黄色
//                main.setConcluRoe("1");
//                conclu.add(1);
//            } else {
//                //得0分，绿色
//                main.setConcluRoe("0");
//                conclu.add(0);
//            }
//        } else {
//            //达到标准值
//            if (roeDesc == 1) {
//                //得2分，橙色
//                main.setConcluRoe("2");
//                conclu.add(2);
//            } else {
//                //得1分，黄色
//                main.setConcluRoe("1");
//                conclu.add(1);
//            }
//        }
        //-----------------------投资收益预警结论---------------------------
        /**
         * [75%,100%]，得0分，绿色
         * [50%,75%]，得1分，黄色
         * [25%,50%]，得2分，橙色
         * [<25%]，得3分，红色
         */
        int sf = main.getInvestDesc().compareTo(new BigDecimal("75"));
        int f = main.getInvestDesc().compareTo(new BigDecimal("50"));
        int tf = main.getInvestDesc().compareTo(new BigDecimal("25"));
        //如果小于25，得3分，红色
        if (tf < 0) {
            main.setConcluInvestDesc("3");
            conclu.add(3);
        }
        //如果大于等于25，并且小于50，得2分，橙色
        if (tf > -1 && f < 0) {
            main.setConcluInvestDesc("2");
            conclu.add(2);
        }
        //如果大于等于50，并且小于75，得1分，黄色
        if (f > -1 && sf < 0) {
            main.setConcluInvestDesc("1");
            conclu.add(1);
        }
        //如果大于等于75，得0分，绿色
        if (sf > -1) {
            main.setConcluInvestDesc("0");
            conclu.add(0);
        }
        //-----------------------跟踪管理预警结论---------------------------
        /**
         * [85%,100%]，得0分，绿色
         * [55%,85%]，得1分，黄色
         * [25%,55%]，得2分，橙色
         * [<25%]，得3分，红色
         */
        int efive = main.getTrackManage().compareTo(new BigDecimal("85"));
        int ffive = main.getTrackManage().compareTo(new BigDecimal("55"));
        int tfive = main.getTrackManage().compareTo(new BigDecimal("25"));
        //如果小于25，得3分，红色
        if (tfive < 0) {
            main.setConcluTrackManage("3");
            conclu.add(3);
        }
        //如果大于等于25，并且小于55，得2分，橙色
        if (tfive > -1 && ffive < 0) {
            main.setConcluTrackManage("2");
            conclu.add(2);
        }
        //如果大于等于55，并且小于85，得1分，黄色
        if (ffive > -1 && efive < 0) {
            main.setConcluTrackManage("1");
            conclu.add(1);
        }
        //如果大于等于85，得0分，绿
        if (efive > -1) {
            main.setConcluTrackManage("0");
            conclu.add(0);
        }

        int sum = (Integer.valueOf(main.getConcluInvestDesc()) + Integer.valueOf(main.getConcluRoe()) + Integer.valueOf(main.getConcluTrackManage())) / 3;
        main.setWarningConclu(String.valueOf(Math.round(sum)));
    }


//    @Override
//    public RiwaEquityInvestVO insertEquityInvest(String mainId){
//        RiwaEquityInvestVO investVO = new RiwaEquityInvestVO();
//        investMainMapper.selectByPrimaryKey(mainId);
//        investVO.setEquityInvestList(investMapper.selectByMainId(mainId));
//        return investVO;
//    }

    @Override
    public Integer save(RiwaEquityInvestMain equityInvest) {
        int i = investMainMapper.updateByPrimaryKeySelective(equityInvest);
        return i;
    }

    @Override
    public RiwaEquityInvestMain selectLastQuarter(String acctYear, String quarter) {
        String acctYear1 = "";
        String quarter1 = "";
        if ("1".equals(acctYear)) {
            acctYear1 = String.valueOf(Integer.valueOf(acctYear) - 1);
            quarter1 = "4";
        } else {
            acctYear1 = acctYear;
            quarter1 = String.valueOf(Integer.valueOf(quarter) - 1);
        }
        RiwaEquityInvestMain riwaEquityInvestMain = investMainMapper.selectQuarter(acctYear1, quarter1);
        if (riwaEquityInvestMain != null) {
            return riwaEquityInvestMain;
        }
        return null;
    }
}
